<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="患者姓名" prop="hzName">
        <el-input
          v-model="queryParams.hzName"
          placeholder="请输入患者姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="诊断号" prop="hzNum">
        <el-input
          v-model="queryParams.hzNum"
          placeholder="请输入诊断号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="药品名0" prop="ypName0">
        <el-input
          v-model="queryParams.ypName0"
          placeholder="请输入药品名0"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格0" prop="ypNorm0">
        <el-input
          v-model="queryParams.ypNorm0"
          placeholder="请输入规格0"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数量0" prop="num0">
        <el-input
          v-model="queryParams.num0"
          placeholder="请输入数量0"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注0" prop="method0">
        <el-input
          v-model="queryParams.method0"
          placeholder="请输入备注0"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="价格0" prop="price0">
        <el-input
          v-model="queryParams.price0"
          placeholder="请输入价格0"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="药品名1" prop="ypName1">
        <el-input
          v-model="queryParams.ypName1"
          placeholder="请输入药品名1"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格1" prop="ypNorm1">
        <el-input
          v-model="queryParams.ypNorm1"
          placeholder="请输入规格1"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退药0" prop="drawal0">
        <el-input
          v-model="queryParams.drawal0"
          placeholder="请输入退药0"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数量1" prop="num1">
        <el-input
          v-model="queryParams.num1"
          placeholder="请输入数量1"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注1" prop="method1">
        <el-input
          v-model="queryParams.method1"
          placeholder="请输入备注1"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="价格1" prop="price1">
        <el-input
          v-model="queryParams.price1"
          placeholder="请输入价格1"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="药品名2" prop="ypName2">
        <el-input
          v-model="queryParams.ypName2"
          placeholder="请输入药品名2"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格2" prop="ypNorm2">
        <el-input
          v-model="queryParams.ypNorm2"
          placeholder="请输入规格2"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数量2" prop="num2">
        <el-input
          v-model="queryParams.num2"
          placeholder="请输入数量2"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退药1" prop="drawal1">
        <el-input
          v-model="queryParams.drawal1"
          placeholder="请输入退药1"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注2" prop="method2">
        <el-input
          v-model="queryParams.method2"
          placeholder="请输入备注2"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="药品名3" prop="ypName3">
        <el-input
          v-model="queryParams.ypName3"
          placeholder="请输入药品名3"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格3" prop="ypNorm3">
        <el-input
          v-model="queryParams.ypNorm3"
          placeholder="请输入规格3"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="价格2" prop="price2">
        <el-input
          v-model="queryParams.price2"
          placeholder="请输入价格2"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数量3" prop="num3">
        <el-input
          v-model="queryParams.num3"
          placeholder="请输入数量3"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注3" prop="method3">
        <el-input
          v-model="queryParams.method3"
          placeholder="请输入备注3"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退药2" prop="drawal2">
        <el-input
          v-model="queryParams.drawal2"
          placeholder="请输入退药2"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="药品名4" prop="ypName4">
        <el-input
          v-model="queryParams.ypName4"
          placeholder="请输入药品名4"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格4" prop="ypNorm4">
        <el-input
          v-model="queryParams.ypNorm4"
          placeholder="请输入规格4"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="价格3" prop="price3">
        <el-input
          v-model="queryParams.price3"
          placeholder="请输入价格3"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数量4" prop="num4">
        <el-input
          v-model="queryParams.num4"
          placeholder="请输入数量4"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注4" prop="method4">
        <el-input
          v-model="queryParams.method4"
          placeholder="请输入备注4"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="价格4" prop="price4">
        <el-input
          v-model="queryParams.price4"
          placeholder="请输入价格4"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退药3" prop="drawal3">
        <el-input
          v-model="queryParams.drawal3"
          placeholder="请输入退药3"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否付费" prop="ypState">
        <el-input
          v-model="queryParams.ypState"
          placeholder="请输入是否付费"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退药4" prop="drawal4">
        <el-input
          v-model="queryParams.drawal4"
          placeholder="请输入退药4"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['medicine:prescription:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['medicine:prescription:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['medicine:prescription:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['medicine:prescription:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="prescriptionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="处方ID" align="center" prop="id" />
      <el-table-column label="患者姓名" align="center" prop="hzName" />
      <el-table-column label="诊断号" align="center" prop="hzNum" />
      <el-table-column label="药品名0" align="center" prop="ypName0" />
      <el-table-column label="规格0" align="center" prop="ypNorm0" />
      <el-table-column label="数量0" align="center" prop="num0" />
      <el-table-column label="备注0" align="center" prop="method0" />
      <el-table-column label="价格0" align="center" prop="price0" />
      <el-table-column label="药品名1" align="center" prop="ypName1" />
      <el-table-column label="规格1" align="center" prop="ypNorm1" />
      <el-table-column label="退药0" align="center" prop="drawal0" />
      <el-table-column label="数量1" align="center" prop="num1" />
      <el-table-column label="备注1" align="center" prop="method1" />
      <el-table-column label="价格1" align="center" prop="price1" />
      <el-table-column label="药品名2" align="center" prop="ypName2" />
      <el-table-column label="规格2" align="center" prop="ypNorm2" />
      <el-table-column label="数量2" align="center" prop="num2" />
      <el-table-column label="退药1" align="center" prop="drawal1" />
      <el-table-column label="备注2" align="center" prop="method2" />
      <el-table-column label="药品名3" align="center" prop="ypName3" />
      <el-table-column label="规格3" align="center" prop="ypNorm3" />
      <el-table-column label="价格2" align="center" prop="price2" />
      <el-table-column label="数量3" align="center" prop="num3" />
      <el-table-column label="备注3" align="center" prop="method3" />
      <el-table-column label="退药2" align="center" prop="drawal2" />
      <el-table-column label="药品名4" align="center" prop="ypName4" />
      <el-table-column label="规格4" align="center" prop="ypNorm4" />
      <el-table-column label="价格3" align="center" prop="price3" />
      <el-table-column label="数量4" align="center" prop="num4" />
      <el-table-column label="备注4" align="center" prop="method4" />
      <el-table-column label="价格4" align="center" prop="price4" />
      <el-table-column label="退药3" align="center" prop="drawal3" />
      <el-table-column label="是否付费" align="center" prop="ypState" />
      <el-table-column label="退药4" align="center" prop="drawal4" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['medicine:prescription:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['medicine:prescription:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改处方对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="患者姓名" prop="hzName">
          <el-input v-model="form.hzName" placeholder="请输入患者姓名" />
        </el-form-item>
        <el-form-item label="诊断号" prop="hzNum">
          <el-input v-model="form.hzNum" placeholder="请输入诊断号" />
        </el-form-item>
        <el-form-item label="药品名0" prop="ypName0">
          <el-input v-model="form.ypName0" placeholder="请输入药品名0" />
        </el-form-item>
        <el-form-item label="规格0" prop="ypNorm0">
          <el-input v-model="form.ypNorm0" placeholder="请输入规格0" />
        </el-form-item>
        <el-form-item label="数量0" prop="num0">
          <el-input v-model="form.num0" placeholder="请输入数量0" />
        </el-form-item>
        <el-form-item label="备注0" prop="method0">
          <el-input v-model="form.method0" placeholder="请输入备注0" />
        </el-form-item>
        <el-form-item label="价格0" prop="price0">
          <el-input v-model="form.price0" placeholder="请输入价格0" />
        </el-form-item>
        <el-form-item label="药品名1" prop="ypName1">
          <el-input v-model="form.ypName1" placeholder="请输入药品名1" />
        </el-form-item>
        <el-form-item label="规格1" prop="ypNorm1">
          <el-input v-model="form.ypNorm1" placeholder="请输入规格1" />
        </el-form-item>
        <el-form-item label="退药0" prop="drawal0">
          <el-input v-model="form.drawal0" placeholder="请输入退药0" />
        </el-form-item>
        <el-form-item label="数量1" prop="num1">
          <el-input v-model="form.num1" placeholder="请输入数量1" />
        </el-form-item>
        <el-form-item label="备注1" prop="method1">
          <el-input v-model="form.method1" placeholder="请输入备注1" />
        </el-form-item>
        <el-form-item label="价格1" prop="price1">
          <el-input v-model="form.price1" placeholder="请输入价格1" />
        </el-form-item>
        <el-form-item label="药品名2" prop="ypName2">
          <el-input v-model="form.ypName2" placeholder="请输入药品名2" />
        </el-form-item>
        <el-form-item label="规格2" prop="ypNorm2">
          <el-input v-model="form.ypNorm2" placeholder="请输入规格2" />
        </el-form-item>
        <el-form-item label="数量2" prop="num2">
          <el-input v-model="form.num2" placeholder="请输入数量2" />
        </el-form-item>
        <el-form-item label="退药1" prop="drawal1">
          <el-input v-model="form.drawal1" placeholder="请输入退药1" />
        </el-form-item>
        <el-form-item label="备注2" prop="method2">
          <el-input v-model="form.method2" placeholder="请输入备注2" />
        </el-form-item>
        <el-form-item label="药品名3" prop="ypName3">
          <el-input v-model="form.ypName3" placeholder="请输入药品名3" />
        </el-form-item>
        <el-form-item label="规格3" prop="ypNorm3">
          <el-input v-model="form.ypNorm3" placeholder="请输入规格3" />
        </el-form-item>
        <el-form-item label="价格2" prop="price2">
          <el-input v-model="form.price2" placeholder="请输入价格2" />
        </el-form-item>
        <el-form-item label="数量3" prop="num3">
          <el-input v-model="form.num3" placeholder="请输入数量3" />
        </el-form-item>
        <el-form-item label="备注3" prop="method3">
          <el-input v-model="form.method3" placeholder="请输入备注3" />
        </el-form-item>
        <el-form-item label="退药2" prop="drawal2">
          <el-input v-model="form.drawal2" placeholder="请输入退药2" />
        </el-form-item>
        <el-form-item label="药品名4" prop="ypName4">
          <el-input v-model="form.ypName4" placeholder="请输入药品名4" />
        </el-form-item>
        <el-form-item label="规格4" prop="ypNorm4">
          <el-input v-model="form.ypNorm4" placeholder="请输入规格4" />
        </el-form-item>
        <el-form-item label="价格3" prop="price3">
          <el-input v-model="form.price3" placeholder="请输入价格3" />
        </el-form-item>
        <el-form-item label="数量4" prop="num4">
          <el-input v-model="form.num4" placeholder="请输入数量4" />
        </el-form-item>
        <el-form-item label="备注4" prop="method4">
          <el-input v-model="form.method4" placeholder="请输入备注4" />
        </el-form-item>
        <el-form-item label="价格4" prop="price4">
          <el-input v-model="form.price4" placeholder="请输入价格4" />
        </el-form-item>
        <el-form-item label="退药3" prop="drawal3">
          <el-input v-model="form.drawal3" placeholder="请输入退药3" />
        </el-form-item>
        <el-form-item label="是否付费" prop="ypState">
          <el-input v-model="form.ypState" placeholder="请输入是否付费" />
        </el-form-item>
        <el-form-item label="退药4" prop="drawal4">
          <el-input v-model="form.drawal4" placeholder="请输入退药4" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPrescription, getPrescription, delPrescription, addPrescription, updatePrescription } from "@/api/medicine/prescription";

export default {
  name: "Prescription",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 处方表格数据
      prescriptionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hzName: null,
        hzNum: null,
        ypName0: null,
        ypNorm0: null,
        num0: null,
        method0: null,
        price0: null,
        ypName1: null,
        ypNorm1: null,
        drawal0: null,
        num1: null,
        method1: null,
        price1: null,
        ypName2: null,
        ypNorm2: null,
        num2: null,
        drawal1: null,
        method2: null,
        ypName3: null,
        ypNorm3: null,
        price2: null,
        num3: null,
        method3: null,
        drawal2: null,
        ypName4: null,
        ypNorm4: null,
        price3: null,
        num4: null,
        method4: null,
        price4: null,
        drawal3: null,
        ypState: null,
        drawal4: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询处方列表 */
    getList() {
      this.loading = true;
      listPrescription(this.queryParams).then(response => {
        this.prescriptionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        hzName: null,
        hzNum: null,
        ypName0: null,
        ypNorm0: null,
        num0: null,
        method0: null,
        price0: null,
        ypName1: null,
        ypNorm1: null,
        drawal0: null,
        num1: null,
        method1: null,
        price1: null,
        ypName2: null,
        ypNorm2: null,
        num2: null,
        drawal1: null,
        method2: null,
        ypName3: null,
        ypNorm3: null,
        price2: null,
        num3: null,
        method3: null,
        drawal2: null,
        ypName4: null,
        ypNorm4: null,
        price3: null,
        num4: null,
        method4: null,
        price4: null,
        drawal3: null,
        ypState: null,
        drawal4: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加处方";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPrescription(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改处方";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePrescription(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPrescription(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除处方编号为"' + ids + '"的数据项？').then(function() {
        return delPrescription(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('medicine/prescription/export', {
        ...this.queryParams
      }, `prescription_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
